import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import store from "@/store";
// 把这段代码直接粘贴到router/index.js中的Vue.use(VueRouter)之前
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch(err => {})
};
Vue.use(VueRouter)


const routes = [{
    path: '/home',
    name: 'home',
    component: Home
  },
  {
    path: '/',
    redirect: './home'
  },
  {
    path: '/order',
    name: 'order',
    component: () => import('../views/Order.vue')
  }, {
    path: '/free',
    name: 'free',
    component: () => import( /* webpackChunkName: "about" */ '../views/Free.vue')
  }, {
    path: '/user',
    name: 'user',
    component: () => import( /* webpackChunkName: "about" */ '../views/User.vue'),
    redirect: '/user/cart',
    children: [{
      path: '/user/cart',
      component: () => import('../components/user/Cart.vue')
    }],
    // beforeEnter: (to, from, next) => {
    //   let token = localStorage.getItem('x-auth-token')
    //   if (token) {
    //     next()
    //   } else {
    //     // alert('请先登录')
    //     store.dispatch('showToast/asyncChangeIsShowToast', {
    //       isShow: true,
    //       title: '请先登录',
    //       type: 'warning'
    //     })
    //   }
    // }
  }, {
    path: '/goods',
    name: 'goods',
    component: () => import( /* webpackChunkName: "about" */ '../views/Goods.vue')
  }, {
    path: '/details',
    name: 'details',
    component: () => import('../views/Details.vue')
  }, {
    path: '*',
    component: () => import('../views/Error.vue')
  },


]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});


// router.beforeEach((to, from, next) => {
//   // to 去哪里
//   // from 从哪里来
//   // next();  没有next  就不会渲染页面
//   if (to.path == '/user') {
//     let token = localStorage.getItem('x-auth-token')
//     if (token) {
//       next()
//     } else {
//       // alert('请先登录')
//       store.dispatch('showToast/asyncChangeIsShowToast', {
//         isShow: true,
//         title: '请先登录',
//         type: 'warning'
//       })
//     }
//   } else {
//     next()
//   }
// })

export default router